package com.ssyc.student.activity;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.OnloadingMoreListener;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.student.R;
import com.ssyc.student.adapter.StFriendAdapter;
import com.ssyc.student.bean.FriendInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 学生端--宠物好友界面
 * Created by Administrator on 2018/5/3 0003.
 */

public class StudentFriendsActivity extends BaseActivity implements View.OnClickListener, SrfManager.OnPullRefreshListener, OnloadingMoreListener {

    //返回按钮
    private ImageView ivBack;

    //加载布局
    private RelativeLayout rlLoading;

    //列表控件
    private RecyclerView rv;

    //下拉刷新控件
    private SmartRefreshLayout srf;

    //数据集合
    private List<FriendInfo.DataBean> oldDatas;

    //适配器
    private StFriendAdapter adapter;

    //空视图
    private View emptyView;

    //分页
    private int page = 1;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.student_activity_friends;
    }

    @Override
    protected void init() {

        showContent();

        //初始化视图
        initView();

        //初始化下拉刷新
        initSrf();

        //初始化列表控件
        initRv();

        //请求服务器
        http(BqaManager.NORML);

    }


    /**
     * 初始化视图的操作
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        rv = findViewById(R.id.rv);
        srf = findViewById(R.id.srf);
        rlLoading = findViewById(R.id.rl_friend_loading);
        rlLoading.setVisibility(View.VISIBLE);
    }

    /**
     * 初始化下拉刷新控件
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }


    /**
     * 初始化列表控件
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        adapter = new StFriendAdapter(this, R.layout.student_rv_friend, oldDatas);
    }


    /**
     * 请求页面数据
     *
     * @param method
     */
    private void http(int method) {

        if (method == BqaManager.NORML || method == BqaManager.PULLREFRESH) {
            page = 1;
        } else {
            page++;
        }

        Map<String, String> map = new HashMap<>();
        map.put("type", "10");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("apptoken", AccountUtils.getToken(this));
        map.put("platform", "8");

        HttpUtils.post(HttpAdress.PETFRIEND, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (null != rlLoading) {
                    rlLoading.setVisibility(View.GONE);
                }
                UiUtils.Toast("网络错误", false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (null != rlLoading) {
                    rlLoading.setVisibility(View.GONE);
                }

                if (!TextUtils.isEmpty(response)) {
                    FriendInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, FriendInfo.class);
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.i("test", "解析数据异常");
                    }
                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //表示返回成功
                            List<FriendInfo.DataBean> newDatas = info.getData();
                            if (method == BqaManager.NORML) {
                                oldDatas.addAll(newDatas);
                                BqaManager.setRv(emptyView, StudentFriendsActivity.this, adapter, rv);
//                              BqaManager.setRv(emptyView, StudentFriendsActivity.this, adapter, rv, StudentFriendsActivity.this);
                            } else {
                                //更新数据
                                BqaManager.updateRv(method, srf, rv, oldDatas, newDatas, adapter);
                            }
                        } else {
                            Log.i("test", info.getState());
                        }
                    }
                }


            }
        });


    }

    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }

    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
        http(BqaManager.LOADINDGMORE);
    }
}
